package com.razorthink.shell.commands.essential;

import java.sql.SQLException;
import java.util.List;

import com.razorthink.shell.core.AbstractCommand;
import com.razorthink.shell.core.EnvironmentVariable;
import com.razorthink.shell.core.entities.User;
import com.razorthink.shell.utils.CommandLogger;
import com.razorthink.shell.utils.DerbyDB;

public class ShowUsers extends AbstractCommand {
	DerbyDB db;

	public ShowUsers(String baseCommand, int params) {
		super(baseCommand, params);
	}

	@Override
	public void execute() {
		try {
			db = DerbyDB.getInstance();
		} catch (SQLException e) {
			CommandLogger.logError(e);
			return;
		}
		if (super.getCommandParser().getParamCount() > 0) {
			CommandLogger.logInfo("showusers command cannot have param");
			return;
		}
		EnvironmentVariable user = super.getEnvVar("User");
		if (user == null) {
			CommandLogger.logInfo("Access not permited");
		} else {
			try {
				List<User> users = db.getAllUsers();
				if (users != null) {
					String list = "";
					for (User usr : users) {
						list += "\n"+usr.getUserName() + "\n";
					}
					CommandLogger.logInfo("list of users "+list);
				} else {
					CommandLogger.logInfo("No users");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	@Override
	public String getDescription() {
		return "It shows list of users(If the person is admin)";
	}

	@Override
	public String getUsage() {
		return "showusers";
	}

}
